package com.bfly.mall.user.service.impl;

import com.bfly.common.util.ValidateUtil;
import com.bfly.core.base.service.BaseServiceImpl;
import com.bfly.core.context.UserThreadLocal;
import com.bfly.core.enums.SysError;
import com.bfly.core.exception.ServiceResponseException;
import com.bfly.mall.system.entity.City;
import com.bfly.mall.system.service.ICityService;
import com.bfly.mall.user.dao.AddressMapper;
import com.bfly.mall.user.entity.Address;
import com.bfly.mall.user.dto.AddressFormDTO;
import com.bfly.mall.user.entity.User;
import com.bfly.mall.user.service.IAddressService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author 长沙商脉科技有限公司
 * @date 2020/9/18 21:22
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS)
public class AddressServiceImpl extends BaseServiceImpl<Address, Integer> implements IAddressService {

    @Autowired
    private AddressMapper addressMapper;
    @Autowired
    private ICityService cityService;

    @Override
    public Address getDefaultAddress(int userId) {
        return addressMapper.getDefaultAddress(userId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(AddressFormDTO formDTO) {
        if (!ValidateUtil.isCellPhone(formDTO.getPhone())) {
            throw new ServiceResponseException(SysError.PARAM_ERROR, "收件人手机号码错误!");
        }
        User user = UserThreadLocal.get();
        Address address = new Address();
        BeanUtils.copyProperties(formDTO, address);

        City city = cityService.get(formDTO.getDistrictId());       //查找区域
        address.setPostCode(city.getZipCode());
        address.setDistrictId(city.getId());
        address.setDistrict(city.getName());

        city = cityService.get(city.getParentId());                 //查找市区
        address.setCityId(city.getId());
        address.setCity(city.getName());

        city = cityService.get(city.getParentId());                 //查找省份
        address.setProvinceId(city.getId());
        address.setProvince(city.getName());

        address.setUserId(user.getId());
        address.setCreateDate(new Date());
        address.setId(0);

        if (formDTO.isDefaultAddress()) {
            // 如果设置为默认地址 则清除已有的默认地址
            Address defaultAddress = getDefaultAddress(user.getId());
            if (defaultAddress != null) {
                defaultAddress.setDefaultAddress(false);
                super.edit(defaultAddress);
            }
        }
        super.save(address);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void edit(AddressFormDTO formDTO) {
        if (!ValidateUtil.isCellPhone(formDTO.getPhone())) {
            throw new ServiceResponseException(SysError.PARAM_ERROR, "收件人手机号码错误!");
        }
        Address address = get(formDTO.getId());
        User user = UserThreadLocal.get();
        if (address == null || address.getUserId() != user.getId()) {
            throw new ServiceResponseException(SysError.PARAM_ERROR, "收货地址不存在!");
        }

        address.setPhone(formDTO.getPhone());
        address.setConsignee(formDTO.getConsignee());
        address.setAddress(formDTO.getAddress());
        City city = cityService.get(formDTO.getDistrictId());       //查找区域
        address.setPostCode(city.getZipCode());
        address.setDistrictId(city.getId());
        address.setDistrict(city.getName());

        city = cityService.get(city.getParentId());                 //查找市区
        address.setCityId(city.getId());
        address.setCity(city.getName());

        city = cityService.get(city.getParentId());                 //查找省份
        address.setProvinceId(city.getId());
        address.setProvince(city.getName());

        address.setUserId(user.getId());
        address.setCreateDate(new Date());

        if (formDTO.isDefaultAddress() && !address.isDefaultAddress()) {
            // 如果设置为默认地址 则清除已有的默认地址
            Address defaultAddress = getDefaultAddress(user.getId());
            if (defaultAddress != null) {
                defaultAddress.setDefaultAddress(false);
                super.edit(defaultAddress);
            }
            address.setDefaultAddress(true);
        }else{
            address.setDefaultAddress(false);
        }
        super.edit(address);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void settingDefaultAddress(int userId, int addressId) {
        Address defaultAddress = getDefaultAddress(userId);
        if (defaultAddress != null) {
            defaultAddress.setDefaultAddress(false);
            super.edit(defaultAddress);
        }
        Address address = get(addressId);
        if (address == null) {
            throw new ServiceResponseException(SysError.PARAM_ERROR, "收货地址不存在!");
        }
        if (address.getUserId() != userId) {
            throw new ServiceResponseException(SysError.PARAM_ERROR, "收货地址不存在!");
        }
        address.setDefaultAddress(true);
        super.edit(address);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeAddress(int userId, int addressId) {
        Address address = get(addressId);
        if (address == null) {
            throw new ServiceResponseException(SysError.PARAM_ERROR, "收货地址不存在!");
        }
        if (address.getUserId() != userId) {
            throw new ServiceResponseException(SysError.PARAM_ERROR, "收货地址不存在!");
        }
        super.remove(addressId);
    }
}
